﻿using System;
using AccelerateSensor.Client.Tools;
using AccelerateSensor.Model.Gateway;
using AccelerateSensor.Service;
using AccelerateSensor.Model.Node;
using ZbSkin.Tools;

namespace AccelerateSensor.Client.Controls
{
    public partial class GatewayControl : PageControl
    {
        public GatewayControl()
        {
            InitializeComponent();
        }

        private void GatewayControl_Load(object sender, EventArgs e)
        {
            DataGridViewHelper.SetCommonStyle(dataGridView);
            DataGridViewHelper.SetDatetimeStyle(dataGridView, "ColumnUpdateTime");
            dataGridView.ColumnHeadersVisible = true;
            ResizeControls();
        }

        private void GatewayControl_Resize(object sender, EventArgs e)
        {
            ResizeControls();
        }

        private void ResizeControls()
        {
            SetRegion();

            dataGridView.Width = Width - 40;
            dataGridView.Height = Height - dataGridView.Top - btnAdd.Height * 2;
            btnAdd.Top = Height - (int) (btnAdd.Height * 1.5f);
            btnDel.Top = btnAdd.Top;
            btnNode.Top = btnAdd.Top;
        }

        private void dataGridView_CellDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            ShowNodePage();
        }

        #region ShowControl/HideControl

        protected override void ShowControl(object data)
        {
            base.ShowControl(data);
            UpdateDataGridView();
            AccelerateSensorService.Instance.GatewayInfoChangedEvent += OnGatewayInfoChanged;
            AccelerateSensorService.Instance.NodeDataChangedEvent += OnNodeDataChanged;
        }

        protected override void HideControl()
        {
            dataGridView.DataSource = null;
            AccelerateSensorService.Instance.GatewayInfoChangedEvent -= OnGatewayInfoChanged;
            AccelerateSensorService.Instance.NodeDataChangedEvent -= OnNodeDataChanged;
            base.HideControl();
        }

        #endregion

        private void OnGatewayInfoChanged(GatewayInfo gatewayInfo)
        {
            Context?.Post((state) => { UpdateDataGridView(); }, null);
        }

        private void OnNodeDataChanged(NodeData nodeData)
        {
            Context?.Post((state) => { UpdateDataGridView(); }, null);
        }

        private void UpdateDataGridView()
        {
            dataGridView.DataSource = null;

            var gatewayList = AccelerateSensorService.Instance.GetGatewayInfoList();
            if (gatewayList.Count == 0)
            {
                gatewayList = AccelerateSensorService.Instance.GatewayInfoList;
            }

            dataGridView.AutoGenerateColumns = false;
            dataGridView.DataSource = gatewayList;
        }

        private void btnNode_Click(object sender, EventArgs e)
        {
            ShowNodePage();
        }

        private void ShowNodePage()
        {
            var info = dataGridView.CurrentRow?.DataBoundItem as GatewayInfo;
            if (info == null)
            {
                return;
            }

            InvokeSwitchPage(new Tuple<string, object>(Constants.PageTag.Node, info.Uuid));
        }
    }
}
